package com.hashmapdemo;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashMapDemo1 {
    public static void main(String[] args) {
        /*
        需求:创建一个HashMap集合，键是学生对象(Student)，值是籍贯(String).
        存储三个键值对元素，并遍历
        要求:同姓名,同年龄认为是同一个学生

        核心点:
            HashMap的键位置如果存储的是自定义对象，需要重写hashcode和equals方法。

         */
        //1.创建HashMap的对象
        HashMap<Student, String> hashMap = new HashMap<>();

        //2.创建学生对象
        Student student1 = new Student("zhangsan", 23);
        Student student2 = new Student("lishi", 24);
        Student student3 = new Student("wangwu", 25);
        Student student4 = new Student("wangwu", 25);

        //3.添加元素
        hashMap.put(student1,"江苏");
        hashMap.put(student2,"浙江");
        hashMap.put(student3,"福建");
        hashMap.put(student4,"山东");

        //4.遍历集合
        Set<Student> keySet = hashMap.keySet();
        for (Student key : keySet) {
            String value = hashMap.get(key);
            System.out.println(key+"="+value);
        }
        System.out.println("=========");

        Set<Map.Entry<Student, String>> entrySet = hashMap.entrySet();
        for (Map.Entry<Student, String> entry : entrySet) {
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"="+value);
        }

        System.out.println("=========");
        hashMap.forEach((key, value) -> System.out.println(key+"="+value));
    }
}
